{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "dis.dis是Python标准库中的一个函数，用于对Python字节码进行反汇编（disassemble）并以可读的方式打印出来。它可以帮助你分析和理解Python代码的底层执行细节。\n",
    "\n",
    "dis.dis函数的语法如下：\n",
    "> dis.dis(codeobj)\n",
    "\n",
    "其中，codeobj是一个代码对象，可以是函数对象、方法对象、代码对象或者可调用对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "* 反汇编函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-25T01:50:13.307067900Z",
     "start_time": "2024-02-25T01:50:13.286249400Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  3           0 RESUME                   0\n",
      "\n",
      "  4           2 LOAD_CONST               1 (1)\n",
      "              4 STORE_FAST               0 (x)\n",
      "\n",
      "  5           6 LOAD_CONST               2 (2)\n",
      "              8 STORE_FAST               1 (y)\n",
      "\n",
      "  6          10 LOAD_FAST                0 (x)\n",
      "             12 LOAD_FAST                1 (y)\n",
      "             14 BINARY_OP                0 (+)\n",
      "             18 STORE_FAST               2 (z)\n",
      "\n",
      "  7          20 LOAD_GLOBAL              1 (NULL + print)\n",
      "             32 LOAD_FAST                2 (z)\n",
      "             34 PRECALL                  1\n",
      "             38 CALL                     1\n",
      "             48 POP_TOP\n",
      "             50 LOAD_CONST               0 (None)\n",
      "             52 RETURN_VALUE\n"
     ]
    }
   ],
   "source": [
    "import dis\n",
    "\n",
    "def my_function():\n",
    "    x = 1\n",
    "    y = 2\n",
    "    z = x + y\n",
    "    print(z)\n",
    "\n",
    "dis.dis(my_function)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "* 反汇编方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-25T01:51:31.137200600Z",
     "start_time": "2024-02-25T01:51:31.125202600Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  4           0 RESUME                   0\n",
      "\n",
      "  5           2 LOAD_CONST               1 (1)\n",
      "              4 STORE_FAST               1 (x)\n",
      "\n",
      "  6           6 LOAD_CONST               2 (2)\n",
      "              8 STORE_FAST               2 (y)\n",
      "\n",
      "  7          10 LOAD_FAST                1 (x)\n",
      "             12 LOAD_FAST                2 (y)\n",
      "             14 BINARY_OP                0 (+)\n",
      "             18 STORE_FAST               3 (z)\n",
      "\n",
      "  8          20 LOAD_GLOBAL              1 (NULL + print)\n",
      "             32 LOAD_FAST                3 (z)\n",
      "             34 PRECALL                  1\n",
      "             38 CALL                     1\n",
      "             48 POP_TOP\n",
      "             50 LOAD_CONST               0 (None)\n",
      "             52 RETURN_VALUE\n"
     ]
    }
   ],
   "source": [
    "import dis\n",
    "\n",
    "class MyClass:\n",
    "    def my_method(self):\n",
    "        x = 1\n",
    "        y = 2\n",
    "        z = x + y\n",
    "        print(z)\n",
    "\n",
    "obj = MyClass()\n",
    "dis.dis(obj.my_method)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
